<?php
namespace soa\mp\cloudPrint\v1;

use soa\client;

class kop  extends client
{
	/**
	 * 服务地址
	 * @var string
	 */
	public $service_url = 'http://kop.kuaidihelp.com/';
	/**
	 * 服务名称
	 * @var string
	 */
	protected $service_name = 'kuaidihelp_kop';

	protected $appId = "51666";
	protected $appKey = "1ad7e8ee6ba6c194f6e7b1d52580b53cd89fe808";
	protected $apiName = "/v1/OrderInfo/OrderSend/order";
	protected $businessType = "";
	protected $data = "";
	protected $brandArr = [
		"sto" => "STO",
		"zt" => "ZTO",
		"sf" => "SF",
		"yd" => "YUNDA",
	];

	private function _handle(){
		$ts = time();
		$sign = md5($this->appId . $this->apiName . $ts . $this->appKey);

		$params = [
			"business_type" => $this->businessType,
			"app_id" => $this->appId,
			"sign" => $sign,
			"ts" => $ts,
			"data" => is_array($this->data) ? json_encode($this->data,JSON_UNESCAPED_UNICODE) : $this->data,
		];

		return $this->post($this->apiName,$params);
	}

	//大客户下单
	public function orderPlace($orderInfo){
		$this->businessType = "3001";
		$this->data = $orderInfo;
		return $this->_handle();
	}

	//验证大客户账号
	public function verifyAccount($brand,$account,$pwd,$extraInfo = []){
		$this->businessType = "3006";
		if("sto" === $brand && is_array($extraInfo) && isset($extraInfo["branch_no"])){
			$branchNo = $extraInfo["branch_no"];
			$shopObj = new \soa\express\shop\shop();
			$shopInfo = $shopObj->getBranchInfoByBranchCode($branchNo,$brand);
			$siteFlag = is_array($shopInfo) && !empty($shopInfo) && isset($shopInfo["name"]) ? $shopInfo["name"] : "";
		}
		$this->data = [
			"customer_name" => $account,
			"customer_password" => $pwd,
			"shipper_type" => $this->shipperType($brand),
			"customer_id" => is_array($extraInfo) && isset($extraInfo["customer_id"]) ? $extraInfo["customer_id"] : "",
			"site_flag" => $siteFlag ? : "",
		];
		return $this->_handle();
	}

	//获取大客户类型
	public function shipperType($brand){
		return isset($this->brandArr[$brand]) && !empty($this->brandArr[$brand]) ? $this->brandArr[$brand] : "";
	}
}